package com.sayimo.maker.mall.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.commons.utils.JSONUtil;
import com.sayimo.maker.commons.utils.RedisUtils;
import com.sayimo.maker.mall.goods.dao.GoodsIndexDao;
import com.sayimo.maker.mall.goods.service.GoodsIndexService;
import com.sayimo.maker.mall.goods.service.GoodsNormsValueStockInterfaceService;
import com.sayimo.maker.mall.goods.vo.GoodsIndex;
import com.sayimo.maker.mall.goods.vo.GoodsIndexDetail;
import com.sayimo.maker.mall.goods.vo.GoodsNormsValueStock;




/**
 * @Copyright (c) 2016 广东省尚一互联网有限公司
 * @Project: maker-mall
 * @ClassName: GoodsIndexModelServiceImpl
 * @Description: 商品首页模板
 * @Author:	张鑫
 * @Date:	日期：2016年1月6日  时间：下午1:56:00
 * @Version 1.0.0
 */
@Service(value = "goodsIndexService")
public class GoodsIndexServiceImpl implements GoodsIndexService {
	
	@Resource(name="goodsIndexDao")
	private  GoodsIndexDao  goodsIndexDao;
	
	@Resource(name="goodsNormsValuesInterfaceService")
	private GoodsNormsValueStockInterfaceService goodsNormsValuesInterfaceService;
	
	@Autowired(required=false)
	private HttpServletRequest request;
	
	private static Log logger = LogFactory.getLog(GoodsIndexServiceImpl.class);
	
	public Map<String, Object> queryGoodsIndexs(String identifier) throws ServiceException{

		Map<String, Object> resultMap = new HashMap<>();
 		try {
 			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
 			
 			String _identifier = StringUtils.isEmpty(identifier) ? "HOME_MODEL" : identifier;
 			String key = RedisUtils.GOODSINDEX_CACHE + _identifier;
 			logger.info("key = " + key);
 			if (RedisUtils.hasKey(key)) {
 				logger.info("get data from cache...");
				List list = RedisUtils.getListCache(key, null, null, Map.class);
				logger.info("list = " + list);
				resultMap.put(Constant.DATA, list);
				return resultMap;
			}
 			
			List<GoodsIndex> goodsIndexs = goodsIndexDao.queryGoodsIndexs();
			if (CollectionUtils.isEmpty(goodsIndexs)) {
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.DATA, "");
				return resultMap;
			}
			
			JSONArray goodsIndexsArray = new JSONArray();
			for (GoodsIndex goodsIndex : goodsIndexs) {
				JSONObject goodsIndexObject = new JSONObject();
				goodsIndexObject.put("name", goodsIndex.getTemplateName());
				goodsIndexObject.put("templateUrl", StringUtils.isNotEmpty(goodsIndex.getForUrl()) ? goodsIndex.getForUrl() : "");
				List<GoodsIndexDetail> goodsIndexDetails = goodsIndex.getGoodsIndexDetails();
				JSONArray photoArray = new JSONArray();
				for (GoodsIndexDetail goodsIndexDetail : goodsIndexDetails) {
					JSONObject photoObject = new JSONObject();
					photoObject.put("photoUrl", goodsIndexDetail.getPhotoUrl());
					Integer type = goodsIndexDetail.getType();
					String goodsClassId = "";
					JSONObject goodsObject = new JSONObject();
					String redirectUrl = "";
					
					if (type == null) {
						photoObject.put("type", "-1");
						photoObject.put("goods", goodsObject);
						photoObject.put("goodsClassId", goodsClassId);
						photoObject.put("redirectUrl", redirectUrl);
						photoArray.put(photoObject);
						goodsIndexObject.put("photos", JSONUtil.readValueToBeanArray(photoArray.toString(), Map.class));
						continue;
					}
					
					switch (type) {
					case 1:
						goodsClassId = String.valueOf(goodsIndexDetail.getRelId());
						break;
					case 2:
						Map<String,Object> goodsNormsValueStockMap = goodsNormsValuesInterfaceService.getFirstGoodsNormsValueStockByGoodsId(goodsIndexDetail.getRelId());
						if (goodsNormsValueStockMap != null) {
							goodsObject.put("goodsId", goodsIndexDetail.getRelId());
							goodsObject.put("normsValueId", goodsNormsValueStockMap.get("goodsNormsValueId"));
						}
						break;
					default:
						redirectUrl = goodsIndexDetail.getUrl();
						break;
					}
					
					photoObject.put("type", type);
					photoObject.put("goods", goodsObject);
					photoObject.put("goodsClassId", goodsClassId);
					photoObject.put("redirectUrl", redirectUrl);
					
					photoArray.put(photoObject);
					goodsIndexObject.put("photos", JSONUtil.readValueToBeanArray(photoArray.toString(), Map.class));
				}
				goodsIndexsArray.put(goodsIndexObject);
			}
			
			// 将值写入redis
			String goodsIndexCache = goodsIndexsArray.toString();
			// logger.info("write goodsIndex into redis cache!");
			// RedisUtils.put(key, goodsIndexCache);
			
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, JSONUtil.readValueToBeanArray(goodsIndexCache, Map.class));
			return resultMap;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
}
